<template>
    <el-container class="base_invoice_info_container">
        <el-main>
            <el-card class="box-card">
                <headerTemplate slot="header" ref="header"
                                :table-ref="tableOptions.ref"
                                :toolbar_buttons="toolbar_buttons"
                                :toolbar_filter="toolbar_filter"
                                :searchForm="searchForm"
                                @event="handleEvent">
                    <template slot="right_search"></template>
                </headerTemplate>
                <vxeTableTemplate :data="dataList" :options.sync="tableOptions"
                                  @event="handleTableEvent" />
            </el-card>
        </el-main>
    </el-container>
</template>

<script>
import provide from "@common/mixins/provide";
import InvoiceInfoForm from "../dialog/InvoiceInfoForm";

export default {
    extends: provide,
    name: "baseInvoice",
    provide() {
        return {
            setChildrenRef: (name, ref) => {
                this[name] = ref;
            },
            getChildrenRef: (name) => {
                return this[name];
            },
            getRef: () => {
                return this;
            },
        };
    },
    data() {
        let _this = this;
        return {
            toolbar_buttons: [
                {
                    id: "refresh",
                    icon: "fa fa-refresh",
                    show: true,
                    method: () => {
                        _this.getList(true);
                    },
                },
                {
                    id: "add",
                    type: "primary",
                    icon: "fa fa-plus",
                    show: true,
                    permission: "xiongda:invoice:save|base:invoice:save",
                    method: () => {
                        _this.openDialog("add", null);
                    },
                },
                {
                    id: "edit",
                    type: "success",
                    icon: "fa fa-pencil",
                    show: true,
                    permission: "xiongda:invoice:save|base:invoice:save",
                    method: () => {
                        _this.openDialog("edit", null);
                    },
                },
                {
                    id: "del",
                    type: "danger",
                    icon: "fa fa-trash-o",
                    show: true,
                    permission: "xiongda:invoice:delete|base:invoice:delete",
                    method: () => {
                        _this.confirmDel();
                    },
                },
            ],
            toolbar_filter: [
                { type: "text", field: "entity_name", label: "发票抬头" },
                { type: "text", field: "entity_code", label: "纳税人识别号" },
                { type: "text", field: "entity_phone", label: "电话" },
                { type: "text", field: "entity_bank", label: "开户行" },
                { type: "text", field: "entity_bank_account", label: "开户账号" },
            ],
            searchForm: {},
            tableOptions: {
                ref: "baseInvoicesTable",
                container: ".base_invoice_info_container",
                multiSelect: true,
                columns: [
                    {
                        label: "发票抬头",
                        prop: "entity_name",
                        align: "center",
                    },
                    {
                        label: "纳税人识别号",
                        prop: "entity_code",
                        align: "center",
                    },
                    {
                        label: "电话",
                        prop: "entity_phone",
                        align: "center",
                    },
                    {
                        label: "地址",
                        prop: "entity_address",
                        align: "center",
                    },
                    {
                        label: "开户行",
                        prop: "entity_bank",
                        align: "center",
                    },
                    {
                        label: "开户账号",
                        prop: "entity_bank_account",
                        align: "center",
                    },
                ],
                operates: {
                    fixed: "right",
                    list: [
                        {
                            id: "edit",
                            icon: "fa fa-pencil",
                            type: "success",
                            show: true,
                            tooltip: {
                                content: "编辑",
                            },
                            permission: "xiongda:invoice:save|base:invoice:save",
                            method: (key, row, index) => {
                                _this.openDialog("edit", row);
                            },
                        },
                        {
                            id: "info",
                            icon: "fa fa-trash-o",
                            type: "danger",
                            tooltip: {
                                content: "删除",
                            },
                            permission: "xiongda:invoice:delete|base:invoice:delete",
                            show: true,
                            method: (key, row, index) => {
                                _this.confirmDel(row);
                            },
                        },
                    ],
                },
                pagination: {
                    pageIndex: 1,
                    pageSize: window.configs.pageSize,
                },
            },
            dataList: [],
            selectionList: [],
        };
    },
    mounted() {
        this.getList(true);
    },
    methods: {
        // 接收搜索条件
        handleEvent(event) {
            let _this = this;
            _this.searchForm = $.extend({}, _this.searchForm, event.searchForm);
            if (event.type === "search") {
                _this.getList(true);
            } else if (event.type === "refresh") {
                _this.getList(true);
            }
        },
        // 获取客户列表
        getList(search) {
            const _this = this;
            _this.dataList = [];
            if (search) {
                _this.tableOptions = _this.resetTablePagination(_this.tableOptions);
            }
            let queryData = _this.getQueryParams(_this.tableOptions, _this.searchForm);
            queryData.owner_sid = -1;
            _this.fetchData(_this.$api.API_FindBaseInvoiceInfos, queryData).then(result => {
                _this.dataList = result.data || [];
                _this.tableOptions = _this.setTableTotal(_this.tableOptions, result);
            }).catch(() => {
            });
        },
        handleTableEvent(event) {
            if (event.type === 'page') {
                this.getList();
                return;
            }
            if (event.type === 'selectionChange') {
                this.selectionList = event.value;
            }
        },
        openDialog(type, row) {
            const _this = this;
            let title = type === "add" ? "创建发票信息" : "编辑发票信息";
            let deepRow = Object.assign({}, row);
            if (!row) {
                if (type === 'add') {
                    deepRow = {};
                }
                if (type === 'edit') {
                    if (!_this.checkSelection(_this.selectionList)) {
                        return;
                    }
                    deepRow = $.extend({}, _this.selectionList[0]);
                }
            }
            _this.$dialog({
                title: title,
                width: '720px',
                component: {
                    value: InvoiceInfoForm,
                    props: {
                        options: {
                            type,
                            row: deepRow,
                        },
                    },
                },
                confirmed() {
                    _this.getList(true);
                },
            });
        },
        // 确认删除用户
        confirmDel(row) {
            let _this = this;
            let sids = [];
            if (!row) {
                if (!_this.checkSelection(_this.selectionList)) {
                    return;
                }
                sids = _this.selectionList.map(item => item.sid);
            } else {
                sids.push(row.sid);
            }
            _this.$confirm("是否继续删除？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                _this.fetchData(_this.$api.API_DeleteBaseInvoiceInfos, {sids: sids}).then(result => {
                    _this.getList(true);
                    _this.$message.success("删除成功");
                }).catch(() => {
                });
            }).catch(() => {
            });
        },
    },
};
</script>
<style lang="less" rel="stylesheet/less" scoped>
.base_invoice_info_container {
    /deep/ .import {
        background-color: #67C23A;
        border-color: #67C23A;
        color: #fff;
    }

    /deep/ .export {
        background-color: #E6A23C;
        border-color: #E6A23C;
        color: #fff;
    }
}
</style>

